/*
*Copyright (c) Nikhil Jain.
*Project ICARUS, communications and 
*artificial intelligence.
*/



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package javadrone.server.simulation;

//~--- non-JDK imports --------------------------------------------------------

import javadrone.command.Priority;
import javadrone.command.commandset.BootStrapCommand;
import javadrone.command.commandset.ManeuverCommand;
import javadrone.command.commandset.QuitCommand;
import javadrone.command.commandset.VideoImageCommand;

import javadrone.remotepackage.RemoteBootPackage;

import javadrone.server.DroneImpl;
import javadrone.server.StateChangeEvent;

import javadrone.statedata.BootData;
import javadrone.statedata.BootData.BootState.BootFlag;

import simbad.sim.EnvironmentDescription;

//~--- JDK imports ------------------------------------------------------------

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.vecmath.Vector3d;

/**
 *
 * @author Nikzzay
 */
public class GenericDroneInstructor extends DroneInstructor {
    private StateFollower follower;

    public GenericDroneInstructor(DroneImpl drone) {
        super(drone);
        follower = new StateFollower(drone);
    }

    @Override
    public void run() {
        System.out.println("Started...");
        drone.putCommand(new BootStrapCommand(new RemoteBootPackage(new EnvironmentDescription())));
        drone.putCommand(new VideoImageCommand(0, Priority.HIGH, VideoImageCommand.Mode.START));

        long i = 0;

        while (true) {
            drone.putCommand(new ManeuverCommand(new Vector3d(0.1, 0.1, 0.1), new Vector3d(0.05, 0.05, 0.05), i * 50,
                    TimeUnit.MILLISECONDS));
            drone.putCommand(new VideoImageCommand(i++ * 10, Priority.HIGH, VideoImageCommand.Mode.CAPTURE));

            if (i >= 20000) {
                break;
            }
        }

        try {
            Thread.sleep(20000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        System.out.println("Kill command issued...");
        drone.putCommand(new VideoImageCommand(Long.MIN_VALUE + 1000000, Priority.HIGH, VideoImageCommand.Mode.STOP));
        drone.putCommand(new QuitCommand());

        while (!follower.bootState.getData().bootState().equals(BootFlag.BOOT_KILLED)) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(GenericDroneInstructor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        System.exit(0);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
